<?php 
/*
# ------------------------------------------------------------------------
# JA Seleni for Joomla 1.5.x - Version 1.0 - Licence Owner JA115884
# ------------------------------------------------------------------------
# Copyright (C) 2004-2009 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
# @license - Copyrighted Commercial Software
# Author: J.O.O.M Solutions Co., Ltd
# Websites:  http://www.joomlart.com -  http://www.joomlancers.com
# This file may not be redistributed in whole or significant part.
# ------------------------------------------------------------------------
*/
  // no direct access
defined('_JEXEC') or die('Restricted access');

require_once (JPATH_SITE . '/components/com_content/helpers/route.php');
/**
 * modJASildeNews class.
 */
class modJASildeNewsHelper {

	/**
	 * @var string $condition;
	 *
	 * @access private
	 */
	var $conditons = '';
	
	/**
	 * @var string $order 
	 *
	 * @access private
	 */
	var $order = 'a.ordering';
	
	/**
	 * @var string $limit
	 *
	 * @access private
	 */
	var $limit  = '';
	
	/**
	 * get listing items from rss link or from list of categories.
	 *
	 * @param JParameter $params
	 * @return array
	 */
	function getList( $params ){
		$rows = array();

		// check cache was endable ?
		if ( $params->get('enable_cache') ) {
			$cache =& JFactory::getCache();
			$cache->setCaching( true );
			$cache->setLifeTime( $params->get( 'cache_time', 30 ) * 60 );	
			$rows = $cache->get( array( $this , 'getArticles' ), array( $params ) ); 
		} else {
			$rows = $this->getArticles( $params );
		}					

		return $rows;
	}
	
	/**
	 * get articles from list of categories and follow up owner paramer.
	 *
	 * @param JParameter $params.
	 * @return array list of articles
	 */
	function getArticles( $params ){
	
		$this->setOrder($params->get('sort_order_field' ,'created'), $params->get('sort_order','DESC'));
		$this->setLimit( $params->get('max_items', 5) );
		$rows = $this->getListArticles( $params );
		return $rows;							
	}
		
	/**
	 * get list articles follow setting configuration.
	 *
	 * @param JParameter $param
	 * @return array 
	 */ 
	function getListArticles( $params ){
	
		global $mainframe; 
		
	 	$db	    = &JFactory::getDBO();
		
		$my = &JFactory::getUser();

		$aid	= $my->get( 'aid', 0 );
		$date =& JFactory::getDate();
		$now  = $date->toMySQL();
		
		$query 	= 	'SELECT a.*,cc.description as catdesc, cc.title as cattitle,s.description as secdesc, s.title as sectitle,' .
					' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
					' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":",cc.id,cc.alias) ELSE cc.id END as catslug,'.
					' CASE WHEN CHAR_LENGTH(s.alias) THEN CONCAT_WS(":", s.id, s.alias) ELSE s.id END as secslug'
					. "\n FROM #__content AS a".
					' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
					' INNER JOIN #__sections AS s ON s.id = a.sectionid'
					. "\n WHERE a.state = 1"
					. "\n AND ( a.publish_up = " . $db->Quote( $db->getNullDate() ) . " OR a.publish_up <= " . $db->Quote( $now  ) . " )"
					. "\n AND ( a.publish_down = " . $db->Quote( $db->getNullDate() ) . " OR a.publish_down >= " . $db->Quote( $now  ) . " )"
					. ( ( !$mainframe->getCfg( 'shownoauth' ) ) ? "\n AND a.access <= " . (int) $aid : '' )
					;
		
		$query .=  $this->getCondition( $params );
		$query .= ' ORDER BY ' . $this->order;

		if( $this->limit ) {
			$query .=  ' LIMIT ' . $this->limit;
		}	

		$db->setQuery($query);
		$data = $db->loadObjectlist();
	
		return $data;
	}
	
	/**
	 * get condition from setting configuration.
	 *
	 * @param JParameter $params
	 * @return string.
	 */
	function getCondition( $params ){
		
		$condition = '';
		if( $params->get("display_model", "modcats") == 'modcats' ) {
			$categories = $params->get( 'display_model-modcats-category' , '' );	
			
			if( $categories != '' ) {
				$ids = $this->getIds( $categories );		
				$condition = " AND cc.id IN($ids)";
			}
		} else {
			$condition = " AND  a.metakey LIKE '%Featured%' ";
		}
		return $condition;
	}
	
	/**
	 * parser options, helper for clause where sql.
	 *
	 * @string array $options
	 * @return string.
	 */
	function getIds( $options ){
		if( !is_array($options) ){
			return (int)$options;
		} else {
			return "'".implode( "','", $options  )."'";
		}		
	}
	
	/**
	 * add sort order sql
	 *
	 * @param string $order is article's field.
	 * @param string $mode is DESC or ASC
	 * @return .
	 */
	function setOrder( $order, $mode ){
		$this->order = ' a.'.$order . ' '. $mode;
		return $this;
	}
	
	/**
	 * add set limit sql
	 * 
	 * @param integer $limit.
	 * @return .
	 */
	function setLimit( $limit ){
		$this->limit = $limit; 
		return $this;
	}
	
	/**
	 * trim string with max specify
	 *  
	 * @param string $title
	 * @param integer $max.
	 */
	function trimString( $title, $max=60  ){

		if( strlen($title) > $max ){
			return substr( $title, 0, $max ) . '...';
		} 
		return $title;
	}
	
	/**
	 * detect and get link with each resource
	 *
	 * @param string $item
	 * @param bool $useRSS.
	 * @return string.
	 */
	function getLink( $item ){
		return  JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catslug, $item->sectionid));
	}
	
	/**
	 *
	 *
	 */
	function renderImage( &$row, $params, $maxchars, $width = 0, $height = 0 ) {
		
			global $database, $_MAMBOTS, $current_charset;
			$image = "";
			$regex = "/\<img.+src\s*=\s*\"([^\"]*)\"[^\>]*\>/";
			preg_match ($regex, $row->text, $matches);
			$align = ($tmp = $params->get("image_alignment","left")) != "auto" ? 'align="'.$tmp.'"' : "";
			$images = (count($matches)) ? $matches : array();
			if (count($images)) $image = $images[1];
		
			if ($image) {
				$thumbnailMode = $params->get( 'thumbnail_mode', 'crop' );
				$aspect 	   = $params->get( 'thumbnail_mode-resize-use_ratio', '1' );
				$crop = $thumbnailMode == 'crop' ? true:false;
				$jaimage = JAImage::getInstance();
				
				if( $thumbnailMode != 'none' && $jaimage->sourceExited($image) ) {
					$imageURL = $jaimage->resize( $image, $width, $height, $crop, $aspect );
					if ( $imageURL == $image ) {
						$width = $width ? "width=\"$width\"" : "";
						$height = $height ? "height=\"$height\"" : "";
						$image = "<img src=\"".JURI::base()."$imageURL\" $align  alt=\"{$row->title}\" title=\"{$row->title}\" $width $height />";
					} else {
						$image = "<img src=\"".JURI::base()."$imageURL\"  $align  alt=\"{$row->title}\" title=\"{$row->title}\" />";
					}
				} else {
					$width = $width ? "width=\"$width\"" : "";
					$height = $height ? "height=\"$height\"" : "";
					$image = "<img src=\"$image\" alt=\"{$row->title}\" $align  title=\"{$row->title}\" $width $height />";	
				}	
			} else {
				$image = '';	
			} 

			$regex1 = "/\<img.*\/\>/";
			$row->introtext = preg_replace( $regex1, '', $row->introtext );
			$row->introtext = trim($row->introtext);
			
			// clean up globals
			return $image;
		}
	}
?>